package com.zhoujing.cinema.front.controller;

import com.github.pagehelper.PageInfo;
import com.zhoujing.cinema.constants.Constant;
import com.zhoujing.cinema.entity.*;
import com.zhoujing.cinema.front.controller.VO.SeatVo;
import com.zhoujing.cinema.service.CinemaService;
import com.zhoujing.cinema.utils.ResponseResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author ：ywj
 * @date ：Created in 2020/3/22 18:05
 * @description：
 * @modified By：
 * @version: $
 */
@Controller
@RequestMapping("/front/cinema")
public class CinemaController {
    @Autowired
    public CinemaService cinemaService;
    @ResponseBody
    @RequestMapping("/getIdByCityName")
    public ResponseResult getIdByCityName(String name){
        City city = cinemaService.findIdByName(name);
        if(ObjectUtils.isEmpty(city)){
            city.setCity_id(1);
            city.setCity_name("南京市");
        }
        ResponseResult responseResult = new ResponseResult();
        responseResult.setData(city);
        responseResult.setMessage("查询成功");
        responseResult.setStatus(Constant.RESPONSE_STATUS_SUCCESS);
        return  responseResult;
    }
    @ResponseBody
    @RequestMapping("/getAllCity")
    public ResponseResult getAllCity(){
        List<City> cityList = cinemaService.findAllCity();
        ResponseResult responseResult = new ResponseResult();
        responseResult.setData(cityList);
        responseResult.setStatus(Constant.RESPONSE_STATUS_SUCCESS);
        responseResult.setMessage("查询成功");
        return responseResult;
    }
    @RequestMapping("/cinema")
    public String goCinema(Integer cityId, Model model,Integer pageNum){
        if(ObjectUtils.isEmpty(pageNum)){
            pageNum=Constant.PAGE_NUM;
        }
        List<CinemaBrand> list1 = cinemaService.findCinemaBrand(cityId);
        List<CinemaArea> list2 = cinemaService.findCinemaArea(cityId);
        List<CinemaType> list3 = cinemaService.findCinemaType(cityId);
        model.addAttribute("data1",list1);
        model.addAttribute("data2",list2);
        model.addAttribute("data3",list3);
        PageInfo<Cinema> list = cinemaService.findCinema(cityId,pageNum,6);
        model.addAttribute("cinemas",list);
        return "cinema";
    }
    @RequestMapping("/getCinemaWithQuery")
    @ResponseBody
    public ResponseResult getCinemaWithQuery(Model model,Integer pageNum,Integer brand,Integer area,String type){
        if (ObjectUtils.isEmpty(type)){
            type=null;
        }
        if(ObjectUtils.isEmpty(pageNum)){
            pageNum=Constant.PAGE_NUM;
        }
        CinemaDto cinemaDto = new CinemaDto();
        cinemaDto.setArea(area);
        cinemaDto.setBrand(brand);
        cinemaDto.setType(type);
        PageInfo<Cinema> cinemaPageInfo = cinemaService.findCinemaWithQuery(cinemaDto,pageNum,6);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setData(cinemaPageInfo);
        responseResult.setStatus(Constant.RESPONSE_STATUS_SUCCESS);
        responseResult.setMessage("查询成功");
        return responseResult;
    }
    @RequestMapping("/cinemaDetail")
    public String goCinemaDetail(Model model,Integer id){
        Cinema cinema = cinemaService.findCinemaById(id);
        model.addAttribute("cinema",cinema);
        Date date = new Date();
        String str = "yyyy-MM-dd";
        SimpleDateFormat sdf = new SimpleDateFormat(str);
        String day = sdf.format(date);
        Session session = new Session();
        session.setDay(day);
        Cinema cinema1 = new Cinema();
        cinema1.setId(id);
        session.setCinema_id(cinema1);
        List<Session> list = cinemaService.findTodaySession(session);
        model.addAttribute("list",list);
        model.addAttribute("num",list.size());
        return "cinemaDetail";
    }
    @ResponseBody
    @RequestMapping("/getSessionById")
    public ResponseResult getSessionById(Integer id){
        ResponseResult responseResult = new ResponseResult();
        Session session = cinemaService.findSessionById(id);
        responseResult.setData(session);
        responseResult.setMessage("查询成功");
        responseResult.setStatus(Constant.RESPONSE_STATUS_SUCCESS);
        return responseResult;
    }
    @RequestMapping("/select")
    public String goSelect(Integer id,Model model){
        return "select";
    }
    @ResponseBody
    @RequestMapping(value = "/buy", method= RequestMethod.POST)
    public ResponseResult buy(@RequestBody SeatVo seat,@CookieValue(value = "userId") Integer userId){
        //System.out.println(seat.toString());
        String booked_seat = StringUtils.join(seat.getBooked_seat(), ",").replace(" ","");
        String seats = StringUtils.join(seat.getSeat(), ",").replace(" ","");
        Session session = new Session();
        session.setId(seat.getSessionId());
        session.setBooked_seat(booked_seat);
        cinemaService.updateSeat(session);
        Order order = new Order();
        order.setUser_id(userId);
        order.setSeat(seats);
        order.setCode(seat.getCode());
        order.setSession_id(seat.getSessionId());
        order.setTotal_price(seat.getTotal());
        //System.out.println(order.toString());
        cinemaService.addOrder(order);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setStatus(Constant.RESPONSE_STATUS_SUCCESS);
        responseResult.setMessage("查询成功");
        return responseResult;
    }
    @ResponseBody
    @RequestMapping("/getOrderList")
    public ResponseResult getOrderList(@CookieValue(value = "userId") Integer userId){
        List<Order1> list = cinemaService.findOrderByUserId(userId);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setData(list);
        return responseResult;
    }
}
